package com.ccut.xunfeiSpringboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ccut.xunfeiSpringboot.entity.Student;
import com.ccut.xunfeiSpringboot.mapper.StudentMapper;
import com.ccut.xunfeiSpringboot.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ccut.xunfeiSpringboot.exception.CustomException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Transactional
    public void add(Student student) {
        String username = student.getUsername();
        Student dbStudent = studentMapper.selectByUsername(username);
        if (dbStudent != null) {
            throw new CustomException("500", "账号已存在，请更换别的账号");
        }
        if (StrUtil.isBlank(student.getPassword())) {
            student.setPassword("123"); // 默认密码
        }
        if (StrUtil.isBlank(student.getUsername())) {
            student.setUsername(student.getUsername());
        }
        studentMapper.insert(student);
    }

    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    public void update(Student student) {
        studentMapper.updateById(student);
    }

    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    public PageInfo<Student> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.selectAll();
        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public Student login(Student student) {
        System.out.println("222");
        String username = student.getUsername();

        Student dbStudent = studentMapper.selectByUsername(username);
        if (dbStudent == null) {
            throw new CustomException("500", "账号不存在");
        }
        String password = student.getPassword();
        if (!dbStudent.getPassword().equals(password)) {
            throw new CustomException("500", "账号或密码错误");
        }
        System.out.println("333");
        return dbStudent;
//        try {
//            return studentMapper.selectByUsernameAndPassword(username, password);
//        } catch (Exception e) {
//            throw new CustomException("500", "系统内部错误，请稍后再试");
//        }
    }

    @Override
    public boolean existsByUsername(String username) {
        return studentMapper.selectByUsername(username) != null;
    }

    @Transactional
    public void register(Student student) {
        this.add(student);
    }

    @Override
    public Student login(String username) {
        Student student = studentMapper.selectByUsername(username);
//        if (student == null) {
//            throw new CustomException("500", "账号不存在");
//        }
        return student;
    }



}